package com.example.t1.view;

import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

/**
 * 
 * 
 * 名称：CommonListView.java 描述：暴漏getview接口给外部，省略了创建adapter类
 * 
 * @author 智迅 zhixun123@gmail.com
 * @date：2013-12-16 上午10:42:02
 * @version v1.0
 */
public class CommonListView<A> extends ListView {

    private Context context;
    private GetViewInterface interface1;

    // private MyAdapter<A> adapter;

    public CommonListView(Context context, AttributeSet attrs, int defStyle) {
	super(context, attrs, defStyle);
	init(context);
    }

    public CommonListView(Context context, AttributeSet attrs) {
	super(context, attrs);
	init(context);
    }

    public CommonListView(Context context) {
	super(context);
	init(context);
    }

    private void init(Context context) {
	this.context = context;

    }

    /**
     * 
     * 描述：插入数据
     * 
     * @param lists
     * @throws
     * @date：2013-12-12 上午11:45:46
     * @author 智迅 zhixun123@gmail.com
     * @version v1.0
     */
    public void setDatas(List<A> lists) {
	MyAdapter<A> adapter = new MyAdapter<A>(context, lists);
	setAdapter(adapter);
    }

    /**
     * 
     * 描述：实现getView
     * 
     * @param interface2
     * @throws
     * @date：2013-12-12 上午11:46:00
     * @author 智迅 zhixun123@gmail.com
     * @version v1.0
     */
    public void getView(GetViewInterface interface2) {
	this.interface1 = interface2;
    }

    /**
     * 
     * 
     * 名称：CommonListView.java 描述：把getview暴漏给外面
     * 
     * @author 智迅 zhixun123@gmail.com
     * @date：2013-12-11 下午5:51:25
     * @version v1.0
     */
    public interface GetViewInterface {

	/**
	 * 
	 * 描述：功能和用法和adapter里面的getView用法一模一样
	 * 
	 * @param position
	 * @param convertView
	 * @param parent
	 * @return
	 * @throws
	 * @date：2013-12-11 下午5:51:55
	 * @author 智迅 zhixun123@gmail.com
	 * @version v1.0
	 */
	public View getView(int position, View convertView, ViewGroup parent);
    }

    private class MyAdapter<A> extends BaseAdapter {

	public List<A> datas;

	// public Context context;
	// public LayoutInflater inflater;

	public MyAdapter(Context context, List<A> data) {
	    // this.context = context;
	    this.datas = data;
	    // inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
	    if (datas == null) {
		return 0;
	    }
	    return datas.size();

	}

	@Override
	public A getItem(int position) {
	    if (datas == null || datas.size() < 1) {
		return null;
	    }
	    return datas.get(position);

	}

	@Override
	public long getItemId(int position) {
	    return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
	    // 模板用代码 ，专供复制
	    // if (convertView == null) {
	    // holder = new ViewHolder();
	    // convertView = inflater.inflate(
	    // android.R.layout.simple_list_item_1, null);
	    //
	    // holder.name = (TextView) convertView
	    // .findViewById(android.R.id.text1);
	    // convertView.setTag(holder);
	    // } else {
	    // holder = (ViewHolder) convertView.getTag();
	    // }
	    return interface1.getView(position, convertView, parent);
	    // return convertView;
	}

    }

}
